﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;
using BCEntity.OA.Workflow.WorkflowActivity;
using Dapper;

namespace BCData.OA.Workflow.WorkflowActivity
{
    public class WorkflowActivityData : IWorkflowActivityData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY = @"SELECT WorkflowActivityId,WorkflowCategoryId,WorkflowKey,WorkflowType,WorkflowName,CompanyId,CreateTime,IsBaseWorkflow
                                        FROM OA_WorkflowActivity";
        public WorkflowActivityData(IDatabaseContext databaseContext)
        {
            this.databaseContext=databaseContext;
        }

        public async Task<bool> CheckExists(string workflowKey)
        {
            string sql = @"SELECT 1 FROM OA_WorkflowActivity WHERE WorkflowKey=?WorkflowKey";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WorkflowKey", workflowKey, DbType.AnsiString, size: 150);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }



        public async Task<WorkflowActivityEntity> GetEntity(string workflowKey)
        {
            string sql = string.Format(@"{0} WHERE WorkflowKey=?WorkflowKey", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WorkflowKey", workflowKey, DbType.AnsiString, size: 150);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<WorkflowActivityEntity>(sql, parameters);
        }

        public async Task<WorkflowActivityEntity> GetEntity(long workflowActivityId)
        {
            string sql = string.Format(@"{0} WHERE WorkflowActivityId=?WorkflowActivityId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WorkflowActivityId", workflowActivityId, DbType.Int64, size: 20);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<WorkflowActivityEntity>(sql, parameters);
        }

        public async Task<(IEnumerable<WorkflowActivityEntity>, int)> Query(long? workflowCategoryId, string workflowName, bool? isBaseWorkflow, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (workflowCategoryId.HasValue)
            {
                wheres.Add("WorkflowCategoryId=?WorkflowCategoryId");
                parameters.Add("?WorkflowCategoryId", workflowCategoryId.Value, DbType.Int64, size: 20);
            }
            if (!string.IsNullOrWhiteSpace(workflowName))
            {
                wheres.Add("WorkflowName LIKE CONCAT('%',CONCAT(?WorkflowName,'%'))");
                parameters.Add("?WorkflowName", workflowName.Trim(), DbType.AnsiString, size: 50);
            }
            if (isBaseWorkflow.HasValue)
            {
                wheres.Add("IsBaseWorkflow=?IsBaseWorkflow");
                parameters.Add("?IsBaseWorkflow", isBaseWorkflow, DbType.Boolean);
            }
            string sql = string.Format("{0} {1} ORDER BY CreateTime DESC LIMIT {2},{3};SELECT COUNT(1) FROM OA_WorkflowActivity {1}",
                QUERY,
                wheres.Count > 0 ? "WHERE " + string.Join(" AND ", wheres) : "",
                (pageIndex - 1) * pageSize, pageSize);
            return await this.databaseContext.Slave.QueryMultipleAsync<WorkflowActivityEntity,int>(sql, parameters);
        }

        public async Task<WorkflowActivityEntity> GetByWorkflowTypeAsync(string workflowType)
        {
            string sql = string.Format("{0} WHERE WorkflowType = ?WorkflowType", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WorkflowType", workflowType, DbType.AnsiString);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<WorkflowActivityEntity>(sql, parameters);
        }
    }
}
